package com.magicsoft.share.ui.share;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListAdapter;
import com.magicsoft.share.adapter.share.UserAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.my.UserList;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.presenter.share.SearchPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.share.ISearchView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends BaseFragment<ISearchView, SearchPresenter> implements ISearchView {


    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    Unbinder unbinder;
    private ArticleListAdapter articleAdapter;
    private UserAdapter userAdapter;
    private String searchType;


    private int pageNum = 1;
    private int pageSize = 10;

    private boolean isRefresh = true;
    private String keyword;
    private int mposition=0;
    private boolean status;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search;
    }

    @Override
    public SearchPresenter initPresenter() {
        return new SearchPresenter();
    }

    @Override
    protected void initData() {
        searchType = getArguments().getString("searchType");

    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //    recyclerView.addItemDecoration(new RecyclerViewDivider(get, LinearLayoutManager.VERTICAL));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(getContext(), 0.5f)));


        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                isRefresh = false;
                pageNum++;
                requestDatas(keyword);
            }
        });
        articleAdapter = new ArticleListAdapter(R.layout.item_article);

        articleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ArticleList.ListBean item = articleAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                intent.putExtra("aticle", item);
                intent.putExtra("articleId",item.getArticleId());
                startActivity(intent);
            }
        });

        articleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_head) {
                    Intent intent = new Intent(getContext(), HomePageActivity.class);
                    intent.putExtra("appUserId",articleAdapter.getItem(position).getAppUserId());
                    startActivity(intent);
                }
            }
        });
        userAdapter = new UserAdapter(R.layout.item_user, 2);
        userAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.btn) {
                    mposition=position;

                    UserList.ListBean item = userAdapter.getItem(position);
                    long otherUserId = item.getAppUserId();
                    if (item.isFollowStatus()) {

                        status=false;
                        initDialog(otherUserId);
                    } else {
                        status=true;
                        mPresenter.addFollow(otherUserId);
                    }
                } else if (view.getId() == R.id.iv_head) {
                    Intent intent = new Intent(getContext(), HomePageActivity.class);
                    intent.putExtra("appUserId",userAdapter.getItem(position).getAppUserId());
                    startActivity(intent);
                }
            }
        });
        if (searchType.equals("文章")) {
            recyclerView.setAdapter(articleAdapter);
        }else {
            recyclerView.setAdapter(userAdapter);
        }

    }

    @Override
    protected void requestData() {

    }


    public void requestDatas(String keyword) {
        this.keyword=keyword;
        if (searchType.equals("文章")) {

            mPresenter.articleList(pageNum,pageSize,keyword);
        } else {

            mPresenter.findUserForHome(keyword, pageNum, pageSize);

        }

    }

    @Override
    public void findUserForHomeSuccess(UserList data) {
        if (isRefresh) {
            userAdapter.setNewData(data.getList());
        } else {
            userAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }
    }

    @Override
    public void findUserForHomeFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    @Override
    public void ArticleListSuccess(ArticleList data) {

        if (isRefresh) {
            articleAdapter.setNewData(data.getList());
        } else {
            articleAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }

    }

    @Override
    public void ArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }



    private void initDialog(final long otherUserId) {
        new AlertView.Builder().setTitle("确认不再关注此人？").
                setContext(getContext()).setCancelText("取消").setOthers(new String[]{"确认"}).
                setStyle(AlertView.Style.Alert).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == 0) {
                    mPresenter.deleteFollow(otherUserId);
                }
            }
        })
                .build().
                show();
    }

    @Override
    public void followChangeSuccess() {

        userAdapter.getItem(mposition).setFollowStatus(status);
        userAdapter.notifyItemChanged(mposition);
    }

}
